package com.sky.controller.admin;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController("adminOrderController")
@Api(tags = {"订单管理接口"})
@Slf4j
@RequestMapping("/admin/order")
public class OrderController {
    @Autowired
    OrderService orderService;
    @GetMapping("/conditionSearch")
    @ApiOperation("分页查询订单")
    public Result<PageResult> list(OrdersPageQueryDTO dto){
        log.info("管理员分页查询订单{}",dto);
        PageResult pageResult = orderService.queryHistoryOrdersAndDishes(dto);
        return Result.success(pageResult);
    }

    @GetMapping("/statistics")
    @ApiOperation("显示订单统计数据")
    public Result<OrderStatisticsVO> countOrder(){
        OrderStatisticsVO orderStatisticsVO = orderService.count();
        log.info("管理员查看订单统计数据{}",orderStatisticsVO);
        return Result.success(orderStatisticsVO);
    }

    @GetMapping("/details/{id}")
    @ApiOperation("查询详细信息")
    public Result<OrderHistoryVO> detailOrders(@PathVariable Long id){
        log.info("管理员查询{}订单详细信息",id);
        OrderHistoryVO ordersVO = orderService.queryOrderDetailByOrderId(id);
        return Result.success(ordersVO);
    }

    @PutMapping("/confirm")
    @ApiOperation("接单")
    public Result acceptOrder(@RequestBody OrdersDTO dto){
        log.info("管理员接受{}订单",dto.getId());
        orderService.acceptOrder(dto.getId());
        return Result.success();
    }

    @PutMapping("/rejection")
    @ApiOperation("拒绝接单")
    public Result rejectOrder(@RequestBody OrdersRejectionDTO dto){
        log.info("管理员拒绝订单，拒单单号{}，拒单原因{}",dto.getId(),dto.getRejectionReason());
        orderService.rejectOrder(dto);
        return Result.success();
    }

    @PutMapping("/cancel")
    @ApiOperation("取消订单")
    public Result cancelOrder(@RequestBody OrdersCancelDTO dto){
        log.info("管理员取消订单，取消单号{}，取消原因{}",dto.getId(),dto.getCancelReason());
        orderService.cancelOrderByAdmin(dto);
        return Result.success();
    }
    @PutMapping("/delivery/{id}")
    @ApiOperation("派送订单")
    public Result deliverOrder(@PathVariable Long id){
        log.info("管理员设置订单{}为派送状态",id);
        orderService.deliverOrder(id);
        return Result.success();
    }

    @PutMapping("/complete/{id}")
    @ApiOperation("完成订单")
    public Result completeOrder(@PathVariable Long id){
        log.info("管理员设置订单{}为完成",id);
        orderService.completeOrder(id);
        return Result.success();
    }
}
